//
//  CustomNewsAmountViewController.m
//  AmountBody
//
//  Created by smm on 2018/1/8.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "CustomNewsAmountViewController.h"
#import "CustomNewsAmountTableViewCell.h"

#import "CustomRulerNewsView.h"
#import "SpecialView.h"
//#import "CustomSketchMapView.h"
#import "CustomSketchMapTableView.h"
#import "CustomNewsShowView.h"
#import "CustomNewsShowMergView.h"
#import "XLBubbleTransition.h"

static const CGFloat Button_Tag = 450000;

@interface CustomNewsAmountViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) UIImageView * tetiImage;
@property (strong, nonatomic) SpecialView * tetiView;
@property (strong, nonatomic) SpecialView * styleView;
@property (strong, nonatomic) CustomRulerNewsView * rulerView;
@property (strong, nonatomic) UIScrollView * size_scrollView;
@property (strong, nonatomic) UIView * size_backView;
@property (strong, nonatomic) UIView * size_lineView;
@property (strong, nonatomic) CustomSketchMapTableView * sketchMapView;
@property (strong, nonatomic) CustomSketchMapTableView * s_sketchMapView;//竖屏显示View
@property (strong, nonatomic) CustomNewsShowMergView * c_newsView;//竖屏时的客户信息view
@property (strong, nonatomic) NSMutableArray * dataArray;
@property (strong, nonatomic) NSMutableArray * newsArray;
@property (strong, nonatomic) UIView * myRightView;
@property (strong, nonatomic) UIScrollView * back_RightScroll;

@end

@implementation CustomNewsAmountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadUI];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

#pragma mark - 懒加载
- (NSMutableArray *)newsArray;
{
    if (!_newsArray) {
        _newsArray = [[NSMutableArray array]init];
        NSArray * nameArr = [NSArray arrayWithObjects:@"客户昵称：",@"客户名称：",@"身高：",@"体重：",@"电话：",@"客户单号：",@"邮箱：",@"备注：", nil];
        NSArray * valueArr = [NSArray arrayWithObjects:@"客户昵称value",@"客户名称value",@"身高value",@"体重value",@"电话value",@"客户单号value",@"邮箱value",@"备注value备注value备注value备注value备注value备注value", nil];
        for (int i=0; i<nameArr.count; i++) {
            EssentialModel * model =[[EssentialModel alloc]init
                                     ];
            model.name = [nameArr objectAtIndex:i];
            model.value = [valueArr objectAtIndex:i];
            [_newsArray addObject:model];
        }
    }
    return _newsArray;
}
- (NSMutableArray *)dataArray;
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray array]init];
        NSArray * nameArr = @[@"西服",@"西裤",@"衬衫"];
        NSArray* typeArr = @[[NSNumber numberWithInt:AmountCellTypeForSize],[NSNumber numberWithInt:AmountCellTypeForSize],[NSNumber numberWithInt:AmountCellTypeForSize]];
        switch (_amountType) {
            case NewsAmountTypeForZZXZ:
            {
                nameArr = @[@"西服",@"西裤",@"衬衫"];
                typeArr = @[[NSNumber numberWithInt:AmountCellTypeForSize],[NSNumber numberWithInt:AmountCellTypeForSize],[NSNumber numberWithInt:AmountCellTypeForSize]];
            }
                break;
            case NewsAmountTypeForZZLT:
            {
                nameArr = @[@"西服",@"西裤",@"衬衫",@"特体和着装"];
                typeArr = @[[NSNumber numberWithInt:AmountCellTypeForHead],[NSNumber numberWithInt:AmountCellTypeForNomal],[NSNumber numberWithInt:AmountCellTypeForNomal],[NSNumber numberWithInt:AmountCellTypeForTeTiAndStyle]];
                
            }
                break;
            case NewsAmountTypeForCYCC:
            {
                nameArr = @[@"西服",@"西裤",@"衬衫",@"特体"];
                typeArr = @[[NSNumber numberWithInt:AmountCellTypeForNomal],[NSNumber numberWithInt:AmountCellTypeForNomal],[NSNumber numberWithInt:AmountCellTypeForNomal],[NSNumber numberWithInt:AmountCellTypeForTeTi]];
            }
                break;
            default:
                break;
        }
        for (int i=0; i<nameArr.count; i++) {
            EssentialModel * model =[[EssentialModel alloc]init];
            model.name = [nameArr objectAtIndex:i];
            model.amountType = [[typeArr objectAtIndex:i] intValue];
            [_dataArray addObject:model];
        }
    }
    return _dataArray;
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [_back_RightScroll mas_remakeConstraints:^(MASConstraintMaker *make) {
        layoutViews(^{//横屏
            make.left.equalTo(self.rightView);
            
        }, ^{
            make.left.equalTo(self.view);
        });
        make.top.bottom.equalTo(self.view);
        make.right.equalTo(self.view).offset(-10);
    }];
    [_sketchMapView mas_updateConstraints:^(MASConstraintMaker *make) {layoutViews(^{//横屏
            _sketchMapView.hidden = NO;
        
        }, ^{
            _sketchMapView.hidden = YES;
           
        });
    }];
    [_s_sketchMapView mas_updateConstraints:^(MASConstraintMaker *make) {
        layoutViews(^{
            _s_sketchMapView.hidden = YES;
            
        }, ^{
            _s_sketchMapView.hidden = NO;
            [self.view bringSubviewToFront:_s_sketchMapView];
        });
    }];
    [_myRightView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        layoutViews(^{
            make.top.left.right.bottom.equalTo(_back_RightScroll);
            make.width.equalTo(self.rightView);
            make.height.equalTo(_back_RightScroll);
        }, ^{
            make.edges.equalTo(_back_RightScroll);
            make.width.equalTo(_back_RightScroll);
            make.height.greaterThanOrEqualTo(@0.f);
        });
        
    }];
    [_c_newsView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        layoutViews(^{
//            [_tableView setTableHeaderView:nil];
            _c_newsView.hidden = YES;
            make.left.equalTo(_myRightView).offset(10);
            make.top.equalTo(_myRightView).offset(10);
            make.right.equalTo(_myRightView).offset(-10);
            make.height.equalTo(@1);
        }, ^{
//            [_tableView setTableHeaderView:_c_newsView];
            _c_newsView.hidden = NO;
            make.left.equalTo(_myRightView).offset(120);
            make.top.equalTo(_myRightView).offset(10);
            make.right.equalTo(_myRightView).offset(-10);
            make.height.equalTo(@160);
        });
//        [_tableView reloadData];
    }];
    
    [_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {layoutViews(^{
            _tableView.scrollEnabled = YES;
            make.top.equalTo(_size_scrollView.mas_bottom);
            make.left.equalTo(_myRightView);
            make.right.equalTo(_myRightView).offset(-20);
            make.bottom.equalTo(_myRightView);
        
        }, ^{
            _tableView.scrollEnabled = NO;
            make.left.equalTo(@(240));
            make.top.equalTo(_c_newsView.mas_bottom);
            make.right.equalTo(@(-10));
            make.height.equalTo(@(_tableView.contentSize.height));
            make.bottom.equalTo(_myRightView);
        });
        
    }];
    if (_amountType == NewsAmountTypeForZZXZ) {
        [_size_scrollView mas_remakeConstraints:^(MASConstraintMaker *make)
            {layoutViews(^{
                make.top.equalTo(@20);
                make.left.equalTo(_myRightView).offset(200);
                make.right.equalTo(_myRightView).offset(-20);
                make.height.equalTo(@(40));
            }, ^{
                make.top.equalTo(_c_newsView.mas_bottom);
                make.width.equalTo(_myRightView).multipliedBy(0.5);
                make.right.equalTo(_myRightView).offset(-20);
                make.height.equalTo(@(40));
            });
            
        }];
    }
    
}
-(void)loadUI
{
    
    _sketchMapView = [[CustomSketchMapTableView alloc]init];
    [self.leftView addSubview:_sketchMapView];
//    [self.view bringSubviewToFront:self.leftView];
    [_sketchMapView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.leftView).insets(UIEdgeInsetsMake(40, 120, 40, 40));
    }];
    _sketchMapView.hasInfo = YES;
    [_sketchMapView loadViewForSketchMap];
    
    _s_sketchMapView = [[CustomSketchMapTableView alloc]init];
    [self.view addSubview:_s_sketchMapView];
    [_s_sketchMapView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(30));
        make.top.equalTo(self.leftView.mas_bottom).offset(20);
        make.width.equalTo(self.view).multipliedBy(0.32);
        make.height.equalTo(self.view).multipliedBy(0.4);
    }];
    _s_sketchMapView.hasInfo = NO;
    [_s_sketchMapView loadViewForSketchMap];
    _back_RightScroll = [[UIScrollView alloc]init];
//    _back_RightScroll.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_back_RightScroll];
    
    _back_RightScroll.showsVerticalScrollIndicator = NO;
    _back_RightScroll.showsHorizontalScrollIndicator = NO;
    _myRightView = [[UIView alloc]init];
    [_back_RightScroll addSubview:_myRightView];
    _tableView = [[UITableView alloc]init];
    [_myRightView addSubview:_tableView];
    
    _c_newsView = [[CustomNewsShowMergView alloc]init];
    [_myRightView addSubview:_c_newsView];
    CGFloat jianGe = 20;
    UIImage * image = [UIImage imageNamed:@"xinxifudongbeijing"];
    // 指定为拉伸模式，伸缩后重新赋值
    image = [image stretchImageWithInsetsTop:jianGe left:jianGe bottom:jianGe right:jianGe];
    _c_newsView.image = image;
    _c_newsView.userInteractionEnabled = YES;
    [_c_newsView loadUIForShowMergeView];
    [_myRightView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_back_RightScroll);
        make.height.equalTo(_back_RightScroll);
        make.width.equalTo(_back_RightScroll);
        
    }];
    self.tableView.estimatedRowHeight = 40;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    [self setNewUIForAmountType];
    
}

-(void)loadScrollForZZXZ
{
    for (UIView * view in _size_backView.subviews) {
        [view removeFromSuperview];
        
    }
    CGFloat scrollH = 40;
    CGFloat jianGe = 20;
    CGFloat buttonH = 35;
    NSArray * nameArray = @[@"MTM标准号",@"欧码",@"英美码",@"亚码",@"澳码"];
    UIButton * l_button = nil;
    for (int i=0; i<nameArray.count; i++) {
        
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(l_button.frame.origin.x+l_button.frame.size.width+jianGe, (scrollH-buttonH)/2.0, 60, buttonH)];
        [_size_backView addSubview:button];
        if (l_button) {
            [button mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(_size_backView);
                make.left.equalTo(l_button.mas_right).offset(jianGe);
                
            }];
        }else{
            [button mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(_size_backView);
                make.left.equalTo(_size_backView).offset(jianGe);
                
            }];
        }
        
        
        [button setTitleColor:ZhongZi_Color forState:UIControlStateNormal];
        [button setTitle:nameArray[i] forState:UIControlStateNormal];
        button.titleLabel.font = Cell_Medium_Font;
//        [button.titleLabel sizeToFit];
//        [button sizeToFit];
        button.tag = Button_Tag+i;
        [button addTarget:self action:@selector(buttonFunc:) forControlEvents:UIControlEventTouchUpInside];
        l_button = button;
    }
    UIButton * tmpButton = [_size_backView viewWithTag:Button_Tag];
    [tmpButton setTitleColor:RGBColorAlpha(255, 66, 89, 1) forState:UIControlStateNormal];
    // 设置过渡视图的右距（此设置将影响到scrollView的contentSize）
    [_size_backView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(l_button.mas_right);
    }];
    _size_lineView = [[UIView alloc]init];
    [_size_backView addSubview:_size_lineView];
    _size_lineView.backgroundColor = RGBColorAlpha(255, 66, 89, 1);
    [_size_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(tmpButton);
        make.left.right.equalTo(tmpButton);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(_size_lineView);
    }];
}

-(void)setNewUIForAmountType
{
    _size_scrollView = [[UIScrollView alloc]init];
    _size_scrollView.backgroundColor = [UIColor whiteColor];
    [_myRightView addSubview:_size_scrollView];
    CGFloat jianGeH = 20;
    switch (_amountType) {
        case NewsAmountTypeForZZXZ:
        {
            
            [_size_scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_myRightView).offset(200);
                make.right.equalTo(_myRightView).offset(-20);
                make.top.equalTo(_myRightView).offset(jianGeH);
                make.height.equalTo(@(40));
            }];
            _size_backView = [[UIView alloc]init];
            [_size_scrollView addSubview:_size_backView];
            [_size_backView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(_size_scrollView);
                make.height.equalTo(_size_scrollView);
                
            }];
            _size_scrollView.showsVerticalScrollIndicator = NO;
            _size_scrollView.showsHorizontalScrollIndicator = NO;
            [self loadScrollForZZXZ];
            [_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_size_scrollView.mas_bottom);
                make.left.right.bottom.equalTo(_myRightView);
            }];
            
        }
            break;
        case NewsAmountTypeForCYCC:
        {
            
        }
            break;
        case NewsAmountTypeForZZLT:
        {
            
        }
            break;
        default:
            break;
    }
}

#pragma mark - 手势按钮方法
-(void)buttonFunc:(UIButton *)button
{
    for (UIButton * tmpButton in _size_backView.subviews) {
        if ([tmpButton isKindOfClass:[UIButton class]]) {
            [tmpButton setTitleColor:ZhongZi_Color forState:UIControlStateNormal];
        }
    }
    [button setTitleColor:RGBColorAlpha(255, 66, 89, 1) forState:UIControlStateNormal];
    [_size_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(button);
        make.left.right.equalTo(button);
        make.height.mas_equalTo(1);
    }];
    [_tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- tableViewDelegate and dataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EssentialModel * model = [self.dataArray objectAtIndex:indexPath.row];
    CustomNewsAmountTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CustomNewsAmountTableViewCell"];
    if (cell == nil) {
        cell = [[CustomNewsAmountTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomNewsAmountTableViewCell"];
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    
    }
    cell.amountType = model.amountType;
    [cell setCellWithModel:model];
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
